% $Id: VelocityAnalyzer.m 3 2008-11-12 15:25:19Z Jie.Bao $

classdef VelocityAnalyzer
%VELOCITYANALYZER gets some statistics abotu velocity for a path.
%   Takes an X,Y,Z,T list of values. The main functions are
%   AverageVelocity(), GetFullSpeedTime(), GetStopTime(),
%   MovingAverage(), and MakeHistogram().

   properties
       X;
       Y;
       Z;
       T;
       Velocities;
       
       %helpers for saving graphs
       SaveHistogram = 0;
       HistogramName = ''
   end

   methods
       % CONSTRUCTOR
       %takes x,y,z, and t points list.
       %calculates a velocity between each concurent set of location
       function obj = VelocityAnalyzer(t,x,y,z)
           obj.X = x;
           obj.Y = y;
           if nargin == 4
               obj.Z = z;
           else
               obj.Z = zeros(length(x), 1);  % 2-D coordinates
           end
           obj.T = t;
           obj = obj.CalcVelocity()';  
       end
       
       
       %MAIN FUNCTIONS 
       
       
       %calculates an average of all the velocities. Sum/count
       function average = AverageVelocity(obj)
           average = sum(obj.Velocities)/length(obj.Velocities);
       end
       
       %calculates the amount of time spent moving at full speed. full
       %speed is btween .15 and .2. These values are just experimental from
       %histograms
       function output = GetFullSpeedTime(this)
           output = 0;
           for j=1:length(this.Velocities)
               if this.Velocities(j) > .15
                   output = output +1;
               end
           end           
       end
       
       %calculates all the time spent at 0 velocity. .03 is considered
       %small enough that its stopped, but there are no moving values
       %between .03 and 0.
       function output = GetStopTime(this)
           output = 0;
           for j=1:length(this.Velocities)
               if this.Velocities(j) < .03
                   output = output +1;
               end
           end           
       end
       
       
       %MovingAverage ignores all values with velocity = 0, so we an see
       %how fast the user ACTUALLY moves.
       function average = MovingAverage(obj)
           timeMoving=0;
           sumVelocities=0;
           for i=1:length(obj.Velocities)
               if (obj.Velocities(i) ~= 0) 
                   timeMoving= timeMoving+1;
                   sumVelocities = sumVelocities+obj.Velocities(i);
               end
           end
           average = sumVelocities/timeMoving;
       end
       
       %creates a histogram of all of the velocities.
       function obj = MakeHistogram(obj)
           hist(obj.Velocities', 0:.02:max(obj.Velocities))
           xlim([0 inf]);
           xlabel('velocity (m/s)', 'FontWeight', 'Bold', 'FontSize', 12);
           ylabel('# event', 'FontWeight', 'Bold', 'FontSize', 12);
           title('Velocity Distribution', 'FontWeight', 'Bold', 'FontSize', 16);
           if (obj.SaveHistogram)
               print('-djpeg',obj.HistogramName)
           end
       end
        
       function obj = Plot(obj)
           scatter(1:length(obj.Velocities), obj.Velocities, 5);
           xlabel('time step', 'FontWeight', 'Bold', 'FontSize', 12);
           ylabel('velocity (m/s)', 'FontWeight', 'Bold', 'FontSize', 12);
       end
       
       %PRIVATE FUNCTONS
       
       %calculates velocities between consecutive points.
       %just distance between 3D points.
       function obj = CalcVelocity(obj)
           for i=1:length(obj.X)-1
               obj.Velocities(i) = distance(obj.X(i), obj.X(i+1),  ...
                                            obj.Y(i), obj.Y(i+1),  ...
                                            obj.Z(i), obj.Z(i+1)) / ...
                                   (obj.T(i+1) - obj.T(i));
           end
       end 
       
       %OBSOLETE 
       %used to determine what to do witht e histogram.
       function obj = set.SaveHistogram(save)
           obj.SaveHistogram = save;
       end
       function obj = set.HistogramName(file_name)
           obj.HistogramName = file_name;
       end
       
   end
end 